﻿#define DEBUG

using System;
using System.Collections.Generic;
using System.Text;

using System.Collections;
using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm.MakingBuffer
{
    class JouetsuNDTakasaki : Common, IRoute 
    {

        public JouetsuNDTakasaki(soDatasets objDSs, Hashtable routeLnWithBfr, soStrings objStrFields)
            : base(objDSs, routeLnWithBfr, objStrFields)
        {
        }

        #region IRoute メンバ

        /// <summary>
        /// 上越と高崎線より、バッファーを作成
        /// </summary>
        /// <param name="objVector">処理元の線路データセットベクトル</param>
        public override void MakeBufferForRoute(soDatasetVector objVector)
        {
            soRecordset objRS = null;
            soRecordset objSubRS = null;
            const int TARGET_ROUTE_COUNT = 2;

            /*該当する処理対象を見当たる*/
            objRS = objVector.Query(
                RouteDataset.FLD_DAIBUN_CD + " = '01' and (" +
                //上越線
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0106' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '495')" +
                " or " +
                //高崎線
                "(" + RouteDataset.FLD_FEATURE_CD + " = '0102' and " +
                RouteDataset.FLD_TSUUSHO_CD + " = '493')" +
                ")",
                false, objStrFields,
                String.Format("group by {0}, {1}, {2} order by {0} desc",
                    RouteDataset.FLD_FEATURE_CD, RouteDataset.FLD_DAIBUN_CD, RouteDataset.FLD_TSUUSHO_CD));

            int mainRouteCount = objRS.RecordCount;
            if (objRS != null && mainRouteCount > 0)
            {
                if (!objRS.IsBOF())
                {
                    objRS.MoveFirst();
                }
#if DEBUG
                soDatasetVector objDSVKirotei = objDSs["キロ標_高崎"] as soDatasetVector;
                soGeoLine objFullRoute = null;
                soGeoLine[] objMultiLines = new soGeoLine[Utility.MAX_MULTIPLE_LINES];
                soGeoLine[] objLinesJouetsu = new soGeoLine[TARGET_ROUTE_COUNT];
                for (int j = 0; j < mainRouteCount; objRS.MoveNext(), j++)
#else
                for (; !objRS.IsEOF(); objRS.MoveNext())
#endif
                {
                    try
                    {
                        feature = objRS.GetFieldValue(RouteDataset.FLD_FEATURE_CD).ToString();
                        daibun = objRS.GetFieldValue(RouteDataset.FLD_DAIBUN_CD).ToString();
                        tsuusho = objRS.GetFieldValue(RouteDataset.FLD_TSUUSHO_CD).ToString();

                        Console.WriteLine(
                            String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}",
                                RouteDataset.FLD_FEATURE_CD, feature,
                                RouteDataset.FLD_DAIBUN_CD, daibun,
                                RouteDataset.FLD_TSUUSHO_CD, tsuusho,
                                j + 1, mainRouteCount));

                        objSubRS = objVector.Query(
                            RouteDataset.FLD_FEATURE_CD + " = '" + feature + "' and " +
                            RouteDataset.FLD_DAIBUN_CD + " = '" + daibun + "' and " +
                            RouteDataset.FLD_TSUUSHO_CD + " = '" + tsuusho + "'",
                            true, null, "");

                        int multiLineCount = objSubRS.RecordCount;
                        if (objSubRS != null && multiLineCount > 0)
                        {
                            if (!objSubRS.IsBOF())
                            {
                                objSubRS.MoveFirst();
                            }

                            /*線路はキロ程を通じたら、キロ程データセットベクトルを保留*/
                            soGeoLine objLineTmp = null;
                            int i = 0;
                            int l = 0;
                            for (; !objSubRS.IsEOF(); objSubRS.MoveNext())
                            {
                                soRecordset objRSContain = null;
                                objLineTmp = objSubRS.GetGeometry() as soGeoLine;
                                objRSContain = objDSVKirotei.QueryEx(objSubRS.GetGeometry(), seSpatialQueryMode.scsCommonPoint, "");
                                if (objRSContain != null)
                                {
                                    if (feature.Equals("0106"))
                                    {
                                        objLinesJouetsu[l++] = objLineTmp;
                                        Marshal.ReleaseComObject(objRSContain);
                                        objRSContain = null;
                                    }
                                    else
                                    { 
                                        objMultiLines[i++] = objLineTmp;
                                    }
                                }
                            }
                            objSubRS.Close();
                            Marshal.ReleaseComObject(objSubRS);
                            objSubRS = null;
                            
                            //Marshal.ReleaseComObject(objLineTmp);
                            //objLineTmp = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        objSubRS.Close();
                        Marshal.ReleaseComObject(objSubRS);
                        objSubRS = null;
                        objRS.Close();
                        Marshal.ReleaseComObject(objRS);
                        objRS = null;

                        throw new Exception(ex.Message);
                    }

                }

                for (int k1 = 0; k1 < TARGET_ROUTE_COUNT; k1++)
                {
                    objFullRoute = objLinesJouetsu[k1];
                    for (int k = 0; objMultiLines[k] != null; k++)
                    {
                        if (objFullRoute.SpatialRelation.Touches(objMultiLines[k] as soGeometry))
                        {
                            if (objFullRoute.Joint(objMultiLines[k]))
                            {
                                Console.WriteLine("joint success!\t" + objFullRoute.Length.ToString());
                                break;
                            }
                        }
                    }
                }

                MakeBufferForPart(objFullRoute, objDSVKirotei, objDSs["キロ標_大宮"] as soDatasetVector, null);

                Marshal.ReleaseComObject(objDSVKirotei);
                objDSVKirotei = null;
            }
        }

        #endregion
    }
}
